# _*_ coding: UTF-8 _*_

# _*_ coding: UTF-8 _*_

from copy import deepcopy

from optimizer import History
from optimizer import Lifecycle


class TCGA:
    """
    Two-part Chromosome Genetic Algorithm on MTSP
    https://doi.org/10.1016/j.ejor.2013.01.043
    [[city sequence], [salesman1, ..., salesman m]] length: n+m
    """
    def __init__(self, data, NP: int, drop_prob: float, p_bts: float, scout: int):

        self.__data, self.__NP, self.__drop_prob, self._p_bts, self.__scout = data, NP, drop_prob, p_bts, scout
        self.__colors, self.__distances = self.__data.colors, self.__data.distances

        self.__colony = []  # solution template: [routes, length, stagnation]
        for i in range(self.__NP):
            self.__colony.append(self.__initialization())
        self.gBest = deepcopy(min(self.__colony, key=lambda x: x[1]))

        self.history = History(pieces=0)
        self.history.update(gBest=self.gBest[1])
        self.life = Lifecycle()

    def optimize(self, iterations=None, evaluations=None, deadline=None):
        self.life.set_stopping_condition(iterations, evaluations, deadline)
        while self.life.check_stopping_condition() is False:
            pass